<?php
/**************************************************/
## Class Name - Model_AdminDetails (Contains all the functions related admin information)
## Created By - Techmodi (19/08/2011)
/**************************************************/

class Model_AdminDetails extends Database {

	function Model_AdminDetails() {
		$this->admindetails = USERS;
		$this->Database();
	}
	
	//function for fetch admin details by userid
	function getAdminDetails() {
		$fields=array('email');    
		$tables=array($this->admindetails);
		$where=array(" id = '1' ");		 
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result['email'];    
	}
	
	## Get user details by userid
	function getAdminUserDetailsByUserId($id) {
		$fields=array();	
		$tables=array($this->admindetails);
		$where=array("id=".$id);
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	//function for listing all admin users  
	function getAllAdminUserByValue($val, $col1='',$col2='',$col3='',$limit='',$offset='') {
		$fields = array();	
		$tables = array($this->admindetails);
		$where = array(" user_type = '2' ");
		if($val) {
			$where[] = "(concat(first_name,' ',last_name) LIKE '%".$val."%' OR email LIKE '%".$val."%' ) AND id != '1'";
		}
		else{
			$where[] = " id != '1' ";
		}
		if($col1 =='0')
		{
			$order = array("username ASC");
		}
		if($col2 =='0')
		{
			$order = array("email ASC");
		}
		if($col3=='0')
		{
			$order = array("registered_date ASC");
		}
		if($col1 =='1')
		{
			$order = array("username DESC");
		}
		if($col2 =='1')
		{
			$order = array("email DESC");
		}
		if($col3=='1')
		{
			$order = array("registered_date DESC");
		}
		$result1 = $this->SelectData( $fields,$tables, $where , $order, $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}	
	//for adding user info in admin user table
	function addAdminUserValue($array) {
		$this->InsertData($this->admindetails,$array);
		$insertId = mysql_insert_id();
		return $insertId;
	}	
	
	// for delete user value by ID
	function deleteUserValueById($userId) {
			$this->DeleteData($this->admindetails,"id",$userId);
	}	
	// Update Admin users records
	function editUserValueById($array, $userId) {
		$this->UpdateData($this->admindetails,$array,"id",$userId,0); 
		$affctcnt = mysql_affected_rows();
		return $affctcnt;
	}
	
	## Update user status with multiple ids
	function updateUserStatus($ids, $status) {
		$sql = "UPDATE ".$this->admindetails." SET user_status='".$status."' WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Delete faq category with multiple ids
	function deleteUsers($ids) {
		$sql = "DELETE FROM ".$this->admindetails." WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	// Globle check username exist or not	
	function chkUserNameExist($username, $id='') {
		$fields=array();
		$tables=array($this->admindetails);
		$where=array("username='".$username."'");
		
		if($id != '') {
			$where[] = " id!=".$id;	
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
	// Globle check email exist or not	
	function chkUserEmailExist($email, $id='') {
		$fields=array('email');
		$tables=array($this->admindetails);
		$where=array("email='".$email."'");
		
		if($id != '') {
			$where[] = " id!=".$id;	
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
	//for resetpassword admin
	function getUserValueByMdUserId($mdid) {
       $fields=array("id,email,username");
        $tables=array($this->admindetails);
        $where=array("md5(id)='$mdid'");
        $result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
        $result= $this->FetchRow($result1);
        return $result;         
    }	
	function checkpassword($id,$pass) {
       $fields=array("id,email");
        $tables=array($this->admindetails);
        $where=array("id='".$id."' AND password ='".md5($pass)."'");
        $result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
        $result= $this->FetchRow($result1);
        return $result;         
    }
	
	//function for listing all admin users  
	function getAllActiveAdminUsers($limit='',$offset='') {
		$fields = array();	
		$tables = array($this->admindetails);
		$where = array(" user_type = '2' AND user_status='1' AND id != '1' ");
		$result1 = $this->SelectData( $fields,$tables, $where , $order = array("id ASC"), $group=array(),$limit,$offset,0);
		$result= $this->FetchAll( $result1 );
		return $result;
	}	
	function getAdminEmailValue(){
		$fields=array("email");
		$tables=array($this->admindetails);
		$where=array("id=1");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result['email'];		
	}
	#FOR Check the duplicate entry
	function getUserEmail($useremail,$id='')
	{
		$fields=array();
		$tables=array($this->admindetails);
		$where=array("email='".$useremail."'");
		if($id)
		{
			$where[] = "id!='".$id."'";
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
	#FOR Check the duplicate entry for tradesman user
	function getTradesmanUserEmail($useremail,$id='')
	{
		$fields=array();
		$tables=array($this->admindetails);
		$where=array("email='".$useremail."' and user_type ='4'");
		if($id)
		{
			$where[] = "id!='".$id."'";
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
	
	#FOR Check the duplicate entry
	function getUserCompanyName($companyname,$id='')
	{
		$fields=array();
		$tables=array($this->admindetails);
		$where=array("companyname='".$companyname."'");
		if($id)
		{
			$where[] = "id!='".$id."'";
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
		
}
?>